using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace LANamp
{
	/// <summary>
	/// Summary description for frmJoin.
	/// </summary>
	public partial class frmJoin : Form
	{
        private LanAmpClient Ampclient;
        private string autoJoin = null;
        
        private void frmJoin_Load(object sender, System.EventArgs e)
		{
            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
            this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
            tbName.Text = System.Environment.UserName;
			Ampclient = new LanAmpClient(this, Application.ProductVersion);
			Ampclient.ClientsUpdated += new ClientList.UpdateEvent(Ampclient_ClientsUpdated);
            Ampclient.ServersUpdated += new ServerList.UpdateEvent(Ampclient_ServersUpdated);
            Ampclient.Rx +=new LanAmpClient.Transmission(Ampclient_Rx);
            Ampclient.Tx += new LanAmpClient.Transmission(Ampclient_Tx);
            Ampclient.WatchedServerChanged += new LanAmpClient.WatchedServerChangedEvent(Ampclient_WatchedServerChanged);
            string newName = "";
            foreach (string arg in System.Environment.GetCommandLineArgs())
            {
                if (arg.Substring(0, 10) == "/nickname=") newName = arg.Substring(10);
                if (arg.Substring(0, 10) == "/autojoin=") autoJoin = arg.Substring(10);
            }
            if (newName != "") tbName.Text = newName;
        }


		private void frmJoin_Closed(object sender, System.EventArgs e)
		{
			Ampclient = null;
		}

        void Ampclient_WatchedServerChanged()
        {
            if (Ampclient.WatchedServer != null)
            {
                // serverinfo ophalen
                labServerName.Text = Ampclient.WatchedServer.Name;
                labServerHost.Text = Ampclient.WatchedServer.Hostname;
                labServerVersion.Text = Ampclient.WatchedServer.Version;
                labServerWinamp.Text = Ampclient.WatchedServer.WinampVersion;
            }
            else
            {
                labServerName.Text = "<lobby>";
                labServerHost.Text = "";
                labServerVersion.Text = "";
                labServerWinamp.Text = "";
            }
            btnConnect.Enabled = (Ampclient.WatchedServer != null);
        }

        private void frmJoin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Ampclient.Kill();
		}

		private void tbName_Enter(object sender, System.EventArgs e)
		{
			tbName.SelectAll();
		}

        private void lvServers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvServers.SelectedIndices.Count > 0)
                Ampclient.WatchedServer = Ampclient.Servers.GetServer(lvServers.SelectedIndices[0]);
            else
                Ampclient.WatchedServer = null;
        }

        private void Ampclient_ClientsUpdated(string serverIP)
        {
            if (Visible)
            {
                string searchServer;
                if (Ampclient.WatchedServer == null)
                {
                    searchServer = "<lobby>";
                }
                else
                {
                    searchServer = Ampclient.WatchedServer.IPaddress;
                }
                if (serverIP == searchServer)
                    {
                        lvClients.Items.Clear();
                        lvClients.BeginUpdate();
                        foreach (Client client in Ampclient.Clients.Items)
                            if (client.Server == searchServer)
                            {
                                ListViewItem lvClient = lvClients.Items.Add(client.Nickname);
                                lvClient.SubItems.Add(client.Host);
                            }
                        lvClients.EndUpdate();
                    }
            }
        }

        void Ampclient_ServersUpdated()
        {
            ListViewItem selItem = null;
            if (Visible)
            {
                lvServers.Items.Clear();
                lvServers.BeginUpdate();
                foreach (Server server in Ampclient.Servers.Items)
                {
                    ListViewItem lvServer = lvServers.Items.Add(server.Name);
                    lvServer.SubItems.Add(server.Host);
                    if (autoJoin != null)
                    {
                        if (server.Name == autoJoin)
                        {
                            selItem = lvServer;
                            autoJoin = null;
                        }
                    }
                }
                lvServers.EndUpdate();
            }
            if (selItem != null)
            {
                lvServers.SelectedIndices.Clear();
                lvServers.SelectedIndices.Add(lvServers.Items.IndexOf(selItem));
                btnConnect_Click(this, null);                
            }
        }

        private void Ampclient_Rx()
        {
            if (Visible)
            {
                Rx.BackColor = Color.Green;
                System.Timers.Timer RxTimer = new System.Timers.Timer();
                RxTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate(object o, System.Timers.ElapsedEventArgs e)
                {
                    Rx.BackColor = Color.Black;
                });
                RxTimer.AutoReset = false;
                RxTimer.Interval = 100;
                RxTimer.Start();
            }
        }

        private void Ampclient_Tx()
        {
            if (Visible)
            {
                Tx.BackColor = Color.Red;
                System.Timers.Timer TxTimer = new System.Timers.Timer();
                TxTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate(object o, System.Timers.ElapsedEventArgs e)
                {
                    Tx.BackColor = Color.Black;
                });
                TxTimer.AutoReset = false;
                TxTimer.Interval = 100;
                TxTimer.Start();
            }
        }

        private void tbName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
               if ((lvServers.SelectedIndices.Count < 1) && (lvServers.Items.Count > 0)) lvServers.SelectedIndices.Add(0);
               btnConnect_Click(sender, e);
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Ampclient.ConnectedServer = Ampclient.WatchedServer;
            /* zou bovenstaande ook werken?
            Ampclient.ConnectedServer = Ampclient.Servers.GetServer(lvServers.SelectedIndices[0]);
            */ 
            frmMain frmMain1 = new frmMain();
            frmMain1.JoinForm = this;
            frmMain1.AmpClient = Ampclient;
            frmMain1.Show();
            this.Hide();
        }

        private void frmJoin_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                Ampclient.WatchedServer = null;
                lvServers.SelectedIndices.Clear();
                Ampclient_ServersUpdated();
                Ampclient_ClientsUpdated("<lobby>");
            }
        }

        private void lvServers_DoubleClick(object sender, EventArgs e)
        {
            if (Ampclient.WatchedServer != null) this.btnConnect_Click(sender, e);
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {
            if (Ampclient != null) Ampclient.Nickname = tbName.Text;
        }

    }
}
